﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web.Mvc;
using Grundy.Entity;
using Grundy.Message;

namespace Grundy.Administration.Web.Services
{
    public class EntitlementService
    {
        public EntitlementsDatabase Db { get; private set; }

        public EntitlementService()
        {
            Db = new EntitlementsDatabase("EntitlementsDatabase");
        }

        public EntitlementService(EntitlementsDatabase db)
        {
            Db = db;
        }

        public IQueryable<LicenseEntitlement> GetLicenseEntitlements(int page = 1, int pageSize = 0)
        {
            if (page == 1 && pageSize == 0)
            {
                return Db.LicenseEntitlements
                    .Include(i => i.License)
                    .Include(i => i.License.Feature)
                    .Include(i => i.Node)
                    .Include(i => i.Product)
                    .Include(i => i.Product.Features)
                    .OrderBy(i => i.Id);
            }
            return Db.LicenseEntitlements
                    .Include(i => i.License)
                    .Include(i => i.License.Feature)
                    .Include(i => i.Node)
                    .Include(i => i.Product)
                    .Include(i => i.Product.Features)
                    .OrderBy(i => i.Id)
                    .Skip((page - 1) * pageSize).Take(pageSize);
        }

        public IQueryable<LicenseEntitlement> GetLicenseEntitlements(IEnumerable<Guid> ids)
        {
            return Db.LicenseEntitlements.Where(i => ids.Contains(i.Id)).Include(i => i.License).Include(i => i.Product);
        }

        public IQueryable<CustomerEntitlement> GetEntitlements(int page = 1, int pageSize = 0)
        {
            if (page == 1 && pageSize == 0)
            {
                return Db.CustomerEntitlements
                   .Include(i => i.LicenseEntitlements)
                   .Include(i => i.Customer)
                   .Include(i => i.LicenseEntitlements.Select(j => j.Product))
                   .Include(i => i.LicenseEntitlements.Select(j => j.License))
                   .Include(i => i.LicenseEntitlements.Select(j => j.License.Feature))
                   .Include(i => i.LicenseEntitlements.Select(j => j.Node))
                   .Include(i => i.LicenseEntitlements.Select(j => j.Node.Identifiers))
                   .OrderBy(i => i.Customer.Name);
            }
            return Db.CustomerEntitlements
                   .Include(i => i.LicenseEntitlements)
                   .Include(i => i.Customer)
                   .Include(i => i.LicenseEntitlements.Select(j => j.Product))
                   .Include(i => i.LicenseEntitlements.Select(j => j.License))
                   .Include(i => i.LicenseEntitlements.Select(j => j.License.Feature))
                   .Include(i => i.LicenseEntitlements.Select(j => j.Node))
                   .Include(i => i.LicenseEntitlements.Select(j => j.Node.Identifiers))
                   .OrderBy(i => i.Customer.Name)
                   .Skip((page - 1) * pageSize).Take(pageSize);
        }

        public CustomerEntitlement GetCustomerEntitlement(Customer customer)
        {
            return Db.CustomerEntitlements
                   .Include(i => i.LicenseEntitlements)
                   .Include(i => i.Customer)
                   .Include(i => i.LicenseEntitlements.Select(j => j.Product))
                   .Include(i => i.LicenseEntitlements.Select(j => j.License))
                   .Include(i => i.LicenseEntitlements.Select(j => j.License.Feature))
                   .Include(i => i.LicenseEntitlements.Select(j => j.Node))
                   .Include(i => i.LicenseEntitlements.Select(j => j.Node.Identifiers))
                   .FirstOrDefault(i => i.Customer.Id == customer.Id);
        }

        public CustomerEntitlement CreateCustomerEntitlement(Customer customer)
        {
            var ce = new CustomerEntitlement {Customer = customer};
            Db.CustomerEntitlements.Add(ce);
            Db.SaveChanges();
            return ce;
        }

        public LicenseEntitlement AddLicenseEntitlement(CustomerEntitlement customerEntitlement, LicenseEntitlement licenseEntitlement)
        {
            customerEntitlement.LicenseEntitlements.Add(licenseEntitlement);
            Db.SaveChanges();
            return licenseEntitlement;
        }

        public void DeleteLicenseEntitlement(Guid customerId, Guid licenseEntitlementId)
        {
            var c = Db.CustomerEntitlements.Find(customerId);
            var le = Db.LicenseEntitlements.Find(licenseEntitlementId);
            c.LicenseEntitlements.Remove(le);
            Db.LicenseEntitlements.Remove(le);
            Db.SaveChanges();
        }
    }
}